package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Region;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class SitReachView extends View {

    private Paint startPaint, line1Paint, line2Paint, line3Paint, line4Paint, line5Paint, endPaint, textPaint;
    private Region startRegion, line1Region, line2Region, line3Region, line4Region, line5Region, endRegion;
    private Path startPath, line1Path, line2Path, line3Path, line4Path, line5Path, endPath;

    // 默认 X 和 Y 值
    private float x = 1200, y = 566.5574f;
    private float x1 = 1050, y1 = 734.4262f;
    private float x2 = 900, y2 = 897.7986f;
    private float x3 = 750, y3 = 1059.6721f;
    private float x4 = 600, y4 = 1230.5386f;
    private float x5 = 350, y5 = 1405.9016f;
    private float xend = 100, yend = 1573.7705f;

    private float ClickX, ClickY;
    private boolean Move, Move1, Move2, Move3, Move4, Move5, MoveEnd;

    public SitReachView(Context context) {
        this(context, null);
    }

    public SitReachView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SitReachView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        @SuppressLint("Recycle") TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LineView, defStyleAttr, 0);

        int startColor = a.getColor(R.styleable.LineView_startColor, Color.RED);
        int line1Color = a.getColor(R.styleable.LineView_line1Color, Color.BLUE);
        int line2Color = a.getColor(R.styleable.LineView_line2Color, Color.GREEN);
        int line3Color = a.getColor(R.styleable.LineView_line3Color, Color.MAGENTA);
        int line4Color = a.getColor(R.styleable.LineView_line4Color, Color.CYAN);
        int line5Color = a.getColor(R.styleable.LineView_line5Color, Color.WHITE);
        int endColor = a.getColor(R.styleable.LineView_endColor, Color.YELLOW);

        float textSize = a.getDimension(R.styleable.LineView_textSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 32, getResources().getDisplayMetrics()));
        a.recycle();

        startPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        startPaint.setColor(startColor);
        startPaint.setStrokeWidth(8.0f);
        startPaint.setTextSize(textSize);

        line1Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        line1Paint.setColor(line1Color);
        line1Paint.setStrokeWidth(8.0f);
        line1Paint.setTextSize(textSize);

        line2Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        line2Paint.setColor(line2Color);
        line2Paint.setStrokeWidth(8.0f);
        line2Paint.setTextSize(textSize);

        line3Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        line3Paint.setColor(line3Color);
        line3Paint.setStrokeWidth(8.0f);
        line3Paint.setTextSize(textSize);

        line4Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        line4Paint.setColor(line4Color);
        line4Paint.setStrokeWidth(8.0f);
        line4Paint.setTextSize(textSize);

        line5Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        line5Paint.setColor(line5Color);
        line5Paint.setStrokeWidth(8.0f);
        line5Paint.setTextSize(textSize);

        endPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        endPaint.setColor(endColor);
        endPaint.setStrokeWidth(8.0f);
        endPaint.setTextSize(textSize);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(textSize);

        startRegion = new Region();
        line1Region = new Region();
        line2Region = new Region();
        line3Region = new Region();
        line4Region = new Region();
        line5Region = new Region();
        endRegion = new Region();

        startPath = new Path();
        line1Path = new Path();
        line2Path = new Path();
        line3Path = new Path();
        line4Path = new Path();
        line5Path = new Path();
        endPath = new Path();

        // 加载存储的 X 和 Y 值
        loadCoordinates();
    }

    private void drawCircleWithLine(Canvas canvas, Path path, Paint paint, Region region, float x, float y, String label) {
        float radius = 20;
        float adjustedX = Math.max(radius, Math.min(x, canvas.getWidth() - radius * 9)); // 确保圆圈不会超出屏幕边界
        float adjustedY = Math.max(radius, Math.min(y, canvas.getHeight() - radius * 2));

        path.reset();
        path.addCircle(adjustedX, adjustedY, radius, Path.Direction.CW);
        region.setPath(path, new Region(0, 0, getMeasuredWidth(), getMeasuredHeight()));
        canvas.drawPath(path, paint);

        if (label == null) {
            label = "";
        }
        float textWidth = paint.measureText(label);
        canvas.drawText(label, adjustedX - textWidth / 2, adjustedY - radius - 10, paint);
        canvas.drawLine(0, adjustedY, getWidth(), adjustedY, paint);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircleWithLine(canvas, startPath, startPaint, startRegion, x, y, "60");
        drawCircleWithLine(canvas, line1Path, line1Paint, line1Region, x1, y1, "50");
        drawCircleWithLine(canvas, line2Path, line2Paint, line2Region, x2, y2, "40");
        drawCircleWithLine(canvas, line3Path, line3Paint, line3Region, x3, y3, "30");
        drawCircleWithLine(canvas, line4Path, line4Paint, line4Region, x4, y4, "20");
        drawCircleWithLine(canvas, line5Path, line5Paint, line5Region, x5, y5, "10");
        drawCircleWithLine(canvas, endPath, endPaint, endRegion, xend, yend, "0");
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        ClickX = event.getX();
        ClickY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Move = checkRegionClick(startRegion, ClickX, ClickY);
                Move1 = checkRegionClick(line1Region, ClickX, ClickY);
                Move2 = checkRegionClick(line2Region, ClickX, ClickY);
                Move3 = checkRegionClick(line3Region, ClickX, ClickY);
                Move4 = checkRegionClick(line4Region, ClickX, ClickY);
                Move5 = checkRegionClick(line5Region, ClickX, ClickY);
                MoveEnd = checkRegionClick(endRegion, ClickX, ClickY);
                break;
            case MotionEvent.ACTION_MOVE:
                if (Move) {
                    x = ClickX;
                    y = ClickY;
                    invalidate();
                }
                if (Move1) {
                    x1 = ClickX;
                    y1 = ClickY;
                    invalidate();
                }
                if (Move2) {
                    x2 = ClickX;
                    y2 = ClickY;
                    invalidate();
                }
                if (Move3) {
                    x3 = ClickX;
                    y3 = ClickY;
                    invalidate();
                }
                if (Move4) {
                    x4 = ClickX;
                    y4 = ClickY;
                    invalidate();
                }
                if (Move5) {
                    x5 = ClickX;
                    y5 = ClickY;
                    invalidate();
                }
                if (MoveEnd) {
                    xend = ClickX;
                    yend = ClickY;
                    invalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                saveCoordinates(); // 存储坐标
                Move = false;
                Move1 = false;
                Move2 = false;
                Move3 = false;
                Move4 = false;
                Move5 = false;
                MoveEnd = false;
                break;
        }
        return true;
    }

    private boolean checkRegionClick(Region region, float x, float y) {
        return region.contains((int) x, (int) y);
    }

    private void saveCoordinates() {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("SitReachPreferences", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putFloat("line1X", x1);
        editor.putFloat("line1Y", y1);
        editor.putFloat("line2X", x2);
        editor.putFloat("line2Y", y2);
        editor.putFloat("line3X", x3);
        editor.putFloat("line3Y", y3);
        editor.putFloat("line4X", x4);
        editor.putFloat("line4Y", y4);
        editor.putFloat("line5X", x5);
        editor.putFloat("line5Y", y5);
        editor.putFloat("endX", xend);
        editor.putFloat("endY", yend);
        editor.apply(); // 提交更改
    }

    private void loadCoordinates() {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences("SitReachPreferences", Context.MODE_PRIVATE);
        x1 = sharedPreferences.getFloat("line1X", 1050);
        y1 = sharedPreferences.getFloat("line1Y", 734.4262f);
        x2 = sharedPreferences.getFloat("line2X", 900);
        y2 = sharedPreferences.getFloat("line2Y", 897.7986f);
        x3 = sharedPreferences.getFloat("line3X", 750);
        y3 = sharedPreferences.getFloat("line3Y", 1059.6721f);
        x4 = sharedPreferences.getFloat("line4X", 600);
        y4 = sharedPreferences.getFloat("line4Y", 1230.5386f);
        x5 = sharedPreferences.getFloat("line5X", 350);
        y5 = sharedPreferences.getFloat("line5Y", 1405.9016f);
        xend = sharedPreferences.getFloat("endX", 100);
        yend = sharedPreferences.getFloat("endY", 1573.7705f);
    }

    public float getYCoordinateOfLabelZero() {
        return yend;
    }

    public float getYCoordinateOfLabelTen() {
        return y5;
    }

    public float getYCoordinateOfLabelTwenty() {
        return y4;
    }

    public float getYCoordinateOfLabelThirty() {
        return y3;
    }

    public float getYCoordinateOfLabelForty() {
        return y2;
    }

    public float getYCoordinateOfLabelFifty() {
        return y1;
    }

    public float getYCoordinateOfLabelSixty() {
        return y;
    }
}
